/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhongketech.sunflower.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.model.SmsCode;
import com.zhongketech.sunflower.transport.model.User;


/**
 * 注册页
 * 
 */
public class RegisterActivity extends BaseActivity {
	private EditText phoneEditText;
	private EditText passwordEditText;
	private EditText smsCodeEditText;
	private EditText userNameEditText;
	private EditText birthdayEditTextEdit;	

	private LinearLayout registerLayout;
	private LinearLayout registerSecondLayout;

	private String phone ;
	private String smsCode;
	private String password;
	private String name ;
	private String gender = "male;";
	private String birthday = "1986-12-01T00:00:00Z";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		phoneEditText = (EditText) findViewById(R.id.phone);
		passwordEditText = (EditText) findViewById(R.id.password);
		smsCodeEditText = (EditText) findViewById(R.id.smsCode);
		birthdayEditTextEdit = (EditText) findViewById(R.id.birthday);
		userNameEditText = (EditText) findViewById(R.id.userName);
		registerLayout = (LinearLayout) findViewById(R.id.registerLayout);
		registerSecondLayout = (LinearLayout) findViewById(R.id.registerSecondLayout);
	}

	/**
	 * 注册
	 * 
	 * @param view
	 */
	public void register(View view) {
		phone = phoneEditText.getText().toString().trim();
		password = passwordEditText.getText().toString().trim();
		smsCode  = smsCodeEditText.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(this, "手机号码不能为空！", Toast.LENGTH_SHORT).show();
			userNameEditText.requestFocus();
			return;
		} else if (TextUtils.isEmpty(password)) {
			Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
			passwordEditText.requestFocus();
			return;
		} else if (TextUtils.isEmpty(smsCode)) {
			Toast.makeText(this, "验证码不能为空！", Toast.LENGTH_SHORT).show();
			smsCodeEditText.requestFocus();
			return;
		}


		registerLayout.setVisibility(View.GONE);
		registerSecondLayout.setVisibility(View.VISIBLE);


	}

	public void getSmsCode(View view)
	{
		phone = phoneEditText.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(this, "手机号码不能为空！", Toast.LENGTH_SHORT).show();
			userNameEditText.requestFocus();
			return;
		}

		final ProgressDialog pd = new ProgressDialog(this);
		pd.setMessage("获取验证码...");
		pd.show();
		new Thread(new Runnable() {
			public void run() {
				SmsCode s = new Action().smsCode(phone);
				pd.dismiss();
				if(null != s)
				{
					smsCode = s.getSmscode();
				}
				else
				{
					MyToast.getInstance().show("获取验证码失败："+phone);
				}


			}
		}).start();


	}

	public void registerSecond(View view)
	{

		name = userNameEditText.getText().toString().trim();
		//		birthday  = birthdayEditTextEdit.getText().toString().trim();
		if (TextUtils.isEmpty(name)) {
			Toast.makeText(this, "用户名不能为空！", Toast.LENGTH_SHORT).show();
			userNameEditText.requestFocus();
			return;
		} 
		//		else if (TextUtils.isEmpty(birthday)) {
		//			Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
		//			passwordEditText.requestFocus();
		//			return;
		//		} else if (TextUtils.isEmpty(smsCode)) {
		//			Toast.makeText(this, "验证码不能为空！", Toast.LENGTH_SHORT).show();
		//			smsCodeEditText.requestFocus();
		//			return;
		//		}

		final ProgressDialog pd = new ProgressDialog(this);
		pd.setMessage("正在注册...");
		pd.show();
		new Thread(new Runnable() {
			public void run() {

				// 调用sdk注册方法
				User user = new Action().signUp(phone, password, gender, name, birthday, smsCode);
				pd.dismiss();
				if(null != user)
				{
					MyApplication.getInstance().setPhone(phone);
					MyApplication.getInstance().setPassword(password);
					MyApplication.getInstance().setVisibleId(user.getVisible_id());
					MyApplication.getInstance().setId(user.getId());
					MyToast.getInstance().show("注册成功");
					finish();
				}
				else
				{
					MyToast.getInstance().show("注册失败："+phone);
				}
			}
		}).start();
	}

	public void back(View view) {
		finish();
	}

}
